<?php
ob_start();
header("content-type:text/html; charset=utf-8");
session_start();
require_once 'inc/config.php';
require_once 'inc/classes/db.php';
require_once 'inc/classes/template.php';
require_once 'inc/classes/util.php';
require_once 'include/staticdata.php';

require_once 'include/topwrap.php';

$tao_config = Taoapi_Config::Init();
$taoapi = new TopWrap(false,true);

$gTemplate['templateDir'] = 'templates/';
$gTemplate['leftDelimiter'] = '{';
$gTemplate['rightDelimiter'] = '}';
$tpl = new Template( $gTemplate );
$db  = new db( $gDb );
util::gpc( );
$tpl->assign('news_static',true);
$tpl->assign('topnav', 	StaticData::getMenuData());
$tpl->assign('smallnav',StaticData::getSmallNav());
$tpl->assign('sort', 	StaticData::getSort());
$tpl->assign('lang',	include('include/lang.php'));
$tpl->register_function('print_img', 'print_img');
$page = !empty($_GET['p']) ? intval($_GET['p']) : 1;
$tpl->assign('page', $page );

$data = array();
$news = $db->fetchrows('select a.f_name as catename, b.* from '.$gDb['prefix'].'news b left join '.$gDb['prefix'].'categories a on b.catid=a.f_id order by b.created desc limit 0, 100');
foreach( $news as $new )
{
	if($new['recommen']) { 
		$data['recommen'][$new['catid']][] = $new; 
	} else {
		$data[$new['catid']][] = $new;
	}
}
function __sort($data) {
	$_tmp = array();
	foreach($data as $new) {
		if( $new['recommen'] ) {
			$_tmp['recommen'][] = $new;
		} else {
			$_tmp[$new['catid']][] = $new;
			$_tmp['raw'][] = $new;
		}
	}
	return $_tmp;
}
$categories = $db->fetchrows("select * from ".$gDb['prefix']."categories");

function getcategory($cates, $fid) {
	$_tmp = array();
	foreach($cates as $cate) {
		if($cate['f_parentid'] == $fid)
			array_push($_tmp, $cate);
	}
	return $_tmp;
}

function listcategory($params) {
	global $categories;
	extract($params);
	$out = '';
	$catid = intval($catid);
	if( $catid ) {
		foreach ($categories as $cate) {
			if($catid == intval($cate['f_parentId'])) {
				$out .= '<a href="newslist.php?catid='.$cate['f_id'].'"> '.$cate['f_name'].' </a>';
			}
		}
	}
	return $out;
}
$tpl->register_function('listcategory','listcategory');

$starts = $taoapi->searchItems('0','明星',1,40);
$tpl->assign('starts',$starts['taobaoke_items']['taobaoke_item']);
$faces  = $taoapi->searchItems('0','美容',1,40);
$tpl->assign('faces',$faces['taobaoke_items']['taobaoke_item']);
$fashi  = $taoapi->searchItems('0','服饰',1,40);
$tpl->assign('fashi',$fashi['taobaoke_items']['taobaoke_item']);

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=1) or b.catid=1 limit 0, 100";
$emotion = $db->fetchrows($sql); //情感
$tpl->assign('emotion', __sort($emotion));
//情感区评论
$sql = "select a.*,b.id as nid from ".$gDb['prefix']."comment a left join ".$gDb['prefix']."news b on a.newsid=b.id where b.catid in (select f_id from ".$gDb['prefix']."categories where f_parentId=1) or b.catid=1 limit 0, 9";
$emotion_comment = $db->fetchrows($sql);
$tpl->assign('emotion_comment',$emotion_comment);

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=20) or b.catid=20 limit 0, 100";
$beauty  = $db->fetchrows($sql); //美容
$tpl->assign('beauty', __sort($beauty));
/*美容区评论*/
$sql = "select a.*,b.id as nid from ".$gDb['prefix']."comment a left join ".$gDb['prefix']."news b on a.newsid=b.id where b.catid in (select f_id from ".$gDb['prefix']."categories where f_parentId=20) or b.catid=20 limit 0, 9";
$beauty_comment = $db->fetchrows($sql);
$tpl->assign('beauty_comment',$beauty_comment);

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=11) or b.catid=11 limit 0, 100";
$fashion = $db->fetchrows($sql); //服饰
$tpl->assign('fashion', __sort($fashion));
/*服饰区评论*/
$sql = "select a.*,b.id as nid from ".$gDb['prefix']."comment a left join ".$gDb['prefix']."news b on a.newsid=b.id where b.catid in (select f_id from ".$gDb['prefix']."categories where f_parentId=11) or b.catid=11 limit 0, 9";
$fashion_comment = $db->fetchrows($sql);
$tpl->assign('fashion_comment',$fashion_comment);

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=32) or b.catid=32 limit 0, 100";
$health  = $db->fetchrows($sql);//健康
$tpl->assign('health', __sort($health));
/*健康区评论*/
$sql = "select a.*,b.id as nid from ".$gDb['prefix']."comment a left join ".$gDb['prefix']."news b on a.newsid=b.id where b.catid in (select f_id from ".$gDb['prefix']."categories where f_parentId=11) or b.catid=11 limit 0, 9";
$health_comment = $db->fetchrows($sql);
$tpl->assign('health_comment',$health_comment);

$sql = "select a.f_name as catename, b.* from ".$gDb['prefix']."news b left join ".$gDb['prefix']."categories a on b.catid=a.f_id where b.catid in(select f_id from ".$gDb['prefix']."categories where f_parentId=43) or b.catid=43 limit 0, 100";
$baby  = $db->fetchrows($sql); //亲子
$tpl->assign('baby', __sort($baby));
/**亲子区评论**/
$sql = "select a.*,b.id as nid from ".$gDb['prefix']."comment a left join ".$gDb['prefix']."news b on a.newsid=b.id where b.catid in (select f_id from ".$gDb['prefix']."categories where f_parentId=43) or b.catid=43 limit 0, 9";
$baby_comment = $db->fetchrows($sql);
$tpl->assign('baby_comment',$baby_comment);

$tpl->assign('data', $data);
$tpl->display( 'news_index.tpl' );
?>